package BackgammonBot;

import BackgammonBot.Algorithmus.AlphaBetaAlgo;
import BackgammonBot.GameIO.IOGameFiles;
import BackgammonBot.GameLogic.BackgammonLogic;


public class Bot 
{
	private long startTime;
	private int roundTime;

    public Bot(int runTime, String input, String output) 
    {
    	this.startTime = System.currentTimeMillis();
    	this.roundTime = runTime;

    	BackgammonLogic game = IOGameFiles.readGameFile(input);
    	AlphaBetaAlgo algo = new AlphaBetaAlgo(2, game);
        
    	System.out.println("Fittness for b: " + algo.getMoveFittness()
                 + " Calctime: " + (int) (System.currentTimeMillis() - this.startTime));
    	
    	StringBuilder sb = new StringBuilder();
    	for (int i = 0; i < algo.getMove().size(); i++) 
    	{
    		// Do moves on board
        	game.doMove(algo.getMove().get(i), game.getInitialPlayer());
    		
			System.out.println("Form: " + algo.getMove().get(i)[0] + " To: " + algo.getMove().get(i)[1]);
//			if(i != 0)
//				sb.append(System.getProperty("line.separator"));
			sb.append(algo.getPlayer() + ":" + algo.getMove().get(i)[0] + "," + algo.getMove().get(i)[1]);
			sb.append(System.getProperty("line.separator"));
		}
    	
    	//Sanity check
    	sb.append(game.printBoard());
    	IOGameFiles.writeGameFile(sb.toString(), output);
    }    

	public int getRemainingTime() 
	{
    	int difference = (int) (System.currentTimeMillis() - this.startTime);
    	int r = this.roundTime - difference;
    	return r;
	}
}
